package models

import (
	"github.com/jinzhu/gorm"
	"kop/libs/database"
	"kop/modules/achievement/configuration/a_type"
	"kop/modules/achievement/entity"
)

func AutoMigrate() {

	database.DB.AutoMigrate(new(entity.Achievement))
}

func GetAchievements(roleID int32) []entity.Achievement {

	var data []entity.Achievement
	if err := database.DB.Where("role_id = ?", roleID).Find(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func GetOne(dB *gorm.DB, roleID int32, Type a_type.Type) (*entity.Achievement, error) {

	var data = &entity.Achievement{
		RoleID: roleID,
		AType:  Type,
	}

	return data, dB.FirstOrCreate(data).Error
}

func Save(dB *gorm.DB, data *entity.Achievement) error {

	return dB.Save(data).Error
}
